﻿using System.Collections.Generic;
using System.Data;
using MedicService.Domain;
using MedicService.Server.Factory.Generic;

namespace MedicService.Server.Factory {
    public class GroupExpMemberFactory : EntityFactory<GroupExpMember> {
        public GroupExpMemberFactory() : base("GroupExpMember", "member") { }

        protected override void FillEntity(GroupExpMember entity, DataRow myDataRow) {
            entity.Id = (int) myDataRow["id"];
            entity.Member = (string)myDataRow["member"];
            entity.GroupExp = new GroupExpFactory().Load(myDataRow["GroupExpId"]);
        }

        protected override string GetInsertQuery(GroupExpMember entity) {
            string q = "INSERT INTO [GroupExpMember] ([member],[GroupExpId])"
                     + " VALUES (" + Nullable(entity.Member)
                                +","+Nullable(entity.GroupExp)+");"
                     + " ";
            return q;
        }

        protected override string GetUpdateQuery(GroupExpMember entity) {
            string q = "UPDATE [GroupExpMember]"
                     + "   SET [member] = " + Nullable(entity.Member)
                     + "      ,[GroupExpId] = " + Nullable(entity.GroupExp)
                     + " WHERE id=" + entity.Id;
            return q;
        }

        public List<GroupExpMember> GetList(GroupExp groupExp) {
            var entities = new List<GroupExpMember>();
            var dataTable = DatabaseConnector.GetData(TableName, OrderBy, " GroupExpId=" + groupExp.Id);
            foreach (DataRow myDataRow in dataTable.Rows) {
                var entity = CreateEntity();
                FillEntity(entity, myDataRow);
                entities.Add(entity);
            }
            return entities;
        }

        protected override GroupExpMember CreateEntity() {
            return new GroupExpMember();
        }

    }
}